﻿using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EventbriteService.Model;

namespace EventbriteService.Linq.Extensibility
{
    public static class EventbriteEnumerable
    {
        public static Decimal TotalPrice(this IEnumerable<Event> events)
        {
            if (events == null)
                throw new ArgumentNullException("events");

            Decimal result = 0;
            foreach (var eve in events)
            {
               foreach(var ticket in eve.TicketClasses)
                    result += ticket.Cost.Value.Value;
            }
            return result;
         }

        public static Decimal MaxPrice(this IEnumerable<Event> events)
        {
            if (events == null)
                throw new ArgumentNullException("events");
            Decimal result = 0;
            foreach (var eve in events)
                foreach (var ticket in eve.TicketClasses)
                    if (result < ticket.Cost.Value)
                        result = ticket.Cost.Value.Value;
            return result;
        }

        public static Decimal MinPrice(this IEnumerable<Event> events)
        {
            if (events == null)
                throw new ArgumentNullException("events");
            Decimal result = 0;
            foreach (var eve in events)
                foreach (var ticket in eve.TicketClasses)
                    if (result > ticket.Cost.Value)
                        result = ticket.Cost.Value.Value;
            return result;
        }

        public static Event MaxEvent(this IEnumerable<Event> source)
        {
            if (source == null)
                throw new ArgumentNullException("surce");
            Event result = null;
            Decimal max = Decimal.MinValue;
            foreach (var eve in source)
            {
                foreach (var ticket in eve.TicketClasses)
                {
                    if (max < ticket.Cost.Value)
                    {
                        max = ticket.Cost.Value.Value;
                        result = eve;
                    }
                }

            }
            return result;
        }

        public static Event MinEvent(this IEnumerable<Event> source)
        {
            if (source == null)
                throw new ArgumentNullException("surce");
            Event result = null;
            Decimal min = decimal.MaxValue;
            foreach (var eve in source)
            {
                foreach (var ticket in eve.TicketClasses)
                {
                    if (min > ticket.Cost.Value)
                    {
                        min = ticket.Cost.Value.Value;
                        result = eve;
                    }
                }

            }
            return result;
        }

        //public static Event MaxSold(this IEnumerable<Event> source)
        //{
        //    if (source == null)
        //        throw new ArgumentNullException("surce");
        //    Event result = null;
        //    int max = int.MinValue;
        //    foreach (var eve in source)
        //    {
        //        foreach (var ticket in eve.TicketClasses)
        //        {
        //            if (max < ticket..QuantitySold)
        //            {
        //                max = ticket.QuantitySold;
        //                result = eve;
        //            }
        //        }

        //    }
        //    return result;
        //}

        //public static Event MinSold(this IEnumerable<Event> source)
        //{
        //    if (source == null)
        //        throw new ArgumentNullException("source");
        //    Event result = null;
        //    int min = int.MaxValue;
        //    foreach (var eve in source)
        //    {
        //        foreach (var ticket in eve.Tickets)
        //        {
        //            if (min > ticket.QuantitySold)
        //            {
        //                min = ticket.QuantitySold;
        //                result = eve;
        //            }
        //        }

        //    }
        //    return result;
        //}
    }
}
